﻿using ePipe;
using System.Runtime.CompilerServices;

namespace Letter.Net.Sockets;

abstract class ASocketSnderSole<TSnder> : ASocketSnderProvider<TSnder> where TSnder : ASocketSnder
{
    protected ASocketSnderSole(PipeScheduler scheduler) : base(scheduler)
    {
        this.snder = this.Create(this.Scheduler);
    }

    private TSnder snder;
    private bool _disposed;

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public override TSnder Rent()
    {
        return this.snder;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public override void Return(TSnder sender)
    {
        sender.Reset();
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public override void Dispose()
    {
        if (!_disposed)
        {
            _disposed = true;
            this.snder.Dispose();

            base.Dispose();
        }
    }
}
